<?php
//首页产品模块控制器
	namespace Application\Admin\Controller;
	use Application\Admin\Model\Productcat as procat;
	use KJ\Upload;
	class Productcat extends Admin{
		public function index(){
		$cat = new procat();
		$this->cat = $cat -> order('id asc') -> select();
		$this -> render();
		}
		
		public function add(){
			if(IS_POST){
				$cat = new procat();
				if($_FILES['image']['size'] != 0){
					$upload = new Upload();
					$result = $upload -> upload('image');
					if(!empty($result['error'])){
						echo "<script>alert('上传信息有误');
						window.history.go(-1);</script>";
						exit;
					}else{
						$cat -> attributes -> procat_src = $result['realPath'];
					}
				}				
				$cat -> attributes -> name = $_POST['cat_name'];
				$cat -> attributes -> href = $_POST['href'];
				if($cat -> save()){
					echo "<script>alert('添加成功');
					window.location.href='index.php?m=admin&c=productcat&a=index';</script>";
					exit;
				}
			}
			$this->render('add');
		}
		
		public function delete(){
			if(IS_GET){
				$id = $_GET['id'];
				$cat = new procat();
				$filename = $cat -> field('procat_src') ->where("id=$id")->select();
				$filename = $filename[0]['procat_src'];
				$cat = $cat -> findModel($id);
				if($cat -> delete()){
					unlink($filename);
					echo "<script>alert('删除成功');
					window.location.href='index.php?m=admin&c=productcat&a=index';</script>";
				}
			}
		}						
		
		public function edit(){
			if(IS_GET){
				$id = $_GET['id'];
				$cat = new procat();
				$this->cat = $cat -> find($id);
				$this->render('edit');
			}
			if(IS_POST){
				$catModel = new procat();
				if($_FILES['image']['size'] != 0){
					$upload = new Upload();
					$result = $upload -> upload('image');
					if(!empty($result['error'])){
						echo "<script>alert('上传信息有误');
						window.history.go(-1);</script>";
						exit;
					}else{
						$catModel -> attributes -> procat_src = $result['realPath'];
					}
				}
				$id = $_POST['id'];
				$cat = $catModel->findModel($id);
				$cat -> attributes -> name = $_POST['cat_name'];
				$cat -> attributes -> href = $_POST['href'];
				if($cat -> save()){
					echo "<script>alert('编辑成功');
					window.location.href='index.php?m=admin&c=productcat&a=index';</script>";
				}
			}
		}		
		
	}
?>